package app.fugema.shop.main.goods.adapter;

import android.view.View;
import android.view.ViewGroup;

import com.base.common.utils.DensityUtil;
import com.base.common.view.adapter.ada.BaseRVAdapter;
import com.base.common.view.adapter.connector.BaseItemMultiType;
import com.base.common.view.adapter.connector.BaseViewHolder;
import app.fugema.shop.R;
import app.fugema.shop.databinding.GoodsClassifyItemBinding;
import app.fugema.shop.main.goods.model.CategorieBean;
import app.fugema.shop.main.goods.view.GoodsClassifyDetailActivity;

public class GoodsClassifyBaseItemMultiType extends BaseItemMultiType<CategorieBean, GoodsClassifyItemBinding> {
    @Override
    public int getLayoutId() {
        return R.layout.goods_classify_item;
    }

    @Override
    public void onBindViewHolder(GoodsClassifyItemBinding binding, int position, BaseViewHolder viewHolder, CategorieBean bean)
    {
        super.onBindViewHolder(binding, position, viewHolder, bean);

        if (viewHolder.getAdapter() != null) {
            int pos = getLastGroupPosition(viewHolder.getAdapter(), position);
            if (pos != -1) {

                if (binding.clItemView.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
                    ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) binding.clItemView.getLayoutParams();
                    int spc = position - pos;
                    if (spc % 3 == 0) {
                        marginLayoutParams.leftMargin = DensityUtil.getDimens(R.dimen.dp_16);
                    } else {
                        marginLayoutParams.leftMargin = DensityUtil.getDimens(R.dimen.dp_19);
                    }
                    binding.clItemView.setLayoutParams(marginLayoutParams);
                }


            }
        }
    }


    @Override
    public void onItemClick(View view, BaseRVAdapter adapter, int onclickType, int position, CategorieBean bean) {
        super.onItemClick(view, adapter, onclickType, position, bean);
        //UIUtils.showToastSafes( "id = " + bean.getId() );
        GoodsClassifyDetailActivity.start(view.getContext(),bean.getId(),bean.getName());

    }

    private int getLastGroupPosition(BaseRVAdapter adapter, int position) {
        int pos = -1;
        for (int i = position - 1; i >= 0; i--) {
            if (adapter.getItemViewType(i) == TYPE_GROUP) {
                pos = i;
                break;
            }
        }
        return pos;
    }


}
